﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

namespace IOS.Common
{
    public class SerializeHelper
    {
        static string validatingStr;

        /// <summary>
        /// 对象初始化，根据XML文件，创建相应的实体对象
        /// </summary>
        /// <param name="objType">需要初始化的对象类型</param>
        /// <param name="filename">用于初始化的XML文件</param>
        /// <returns>Object 对象</returns>
        public static Object Init(Type objType, string filename)
        {
            // 根据XML文件格式化XSD对象
            XmlSerializer xmlserial = new XmlSerializer(objType);
            XmlTextReader xReader = new XmlTextReader(filename);
            try
            {
                return xmlserial.Deserialize(xReader);
            }
            finally
            {
                xReader.Close();
            }
        }

        public static void Serialize(Object obj, Type type)
        {
            System.Xml.XmlTextWriter w = null;
            try
            {
                System.Xml.Serialization.XmlSerializer x = new XmlSerializer(type);
                w = new XmlTextWriter("e:\\2.xml", System.Text.Encoding.Default);
                x.Serialize(w, obj);


            }
            catch (XmlException ex)
            {
                throw new XmlException(ex.Message);
            }
            catch (System.Exception ex)
            {
                throw new System.Exception(ex.Message);
            }
            finally
            {
                w.Flush();
                w.Close();

            }

        }


        /// <summary>
        /// 对象初始化，指定字符串进行读取，创建相应的实体对象
        /// </summary>
        /// <example>
        /// <code>
        /// User uData = InitByString(typeof(User), userStr) as User;
        /// </code>
        /// </example>
        /// <param name="objType">需要初始化的对象类型</param>
        /// <param name="objStr">用于初始化的字符串</param>
        /// <returns>Object 对象</returns>
        public static object InitByString(Type objType, string objStr)
        {
            XmlSerializer xmlserial = new XmlSerializer(objType);
            TextReader tReader = new StringReader(objStr);
            try
            {
                return xmlserial.Deserialize(tReader);
            }
            finally
            {
                tReader.Close();
            }
        }


     

        /// <summary>
        /// 以字符串形式格式化输出对象
        /// </summary>
        /// <param name="obj">对象</param>
        /// <returns>对象的字符串表示</returns>
        public static string Format(object obj)
        {
            XmlSerializer xmlserial = new XmlSerializer(obj.GetType());
            MemoryStream mStream = new MemoryStream();
            TextWriter tWriter = new StreamWriter(mStream, Encoding.GetEncoding("GB2312"));
            try
            {
                xmlserial.Serialize(tWriter, obj);
                Encoding utf = Encoding.GetEncoding("GB2312");
                return utf.GetString(mStream.ToArray());
            }

            finally
            {
                mStream.Close();
                tWriter.Close();
            }
        }

       
    }
}
